package cz.muni.fi.pa165.travelagency.web.wsapi;

import cz.muni.fi.pa165.travelagency.api.entities.IPackageTour;
import cz.muni.fi.pa165.travelagency.api.services.IPackageTourService;
import cz.muni.fi.pa165.travelagency.dto.PackageTourDTO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;

/**
 *
 * @author lukas
 */
@WebService(serviceName = "PTourService", wsdlLocation = "PTourService.wsdl")
public class PTourService {

    public PTourService() {
        SecurityUtils.getSubject().login(new UsernamePasswordToken("rest", "rest", true));
    }
    
    @EJB
    private IPackageTourService ejbRef;
    
    @WebMethod(operationName = "createPackageTour")
    @Oneway
    public void createPackageTour(@WebParam(name = "pt") PackageTourDTO pt) {
        ejbRef.createPackageTour(pt);
    }
    
    @WebMethod(operationName = "deletePackageTour")
    @Oneway
    public void deletePackageTour(@WebParam(name = "pt") PackageTourDTO pt) {
        ejbRef.deletePackageTour(pt);
    }
    
    @WebMethod(operationName = "findPackageTour")
    public List<PackageTourDTO> find(@WebParam(name = "destination") String destination, @WebParam(name = "maxPrice") int maxPrice, @WebParam(name = "dateFrom") Date dateFrom, @WebParam(name = "dateTo") Date dateTo, @WebParam(name = "numberOfPersons") int numberOfPersons) {
        List<PackageTourDTO> found = new ArrayList<>();
        for (IPackageTour ipt : ejbRef.find(destination, maxPrice, dateFrom, dateTo, numberOfPersons)) {
            found.add((PackageTourDTO) ipt);
        }
        return found;
    }
    
    @WebMethod(operationName = "findAllPackageTours")
    public List<PackageTourDTO> findAll() {
        List<PackageTourDTO> all = new ArrayList<>();
        for (IPackageTour ipt : ejbRef.findAll()) {
            all.add((PackageTourDTO) ipt);
        }
        return all;
    }
    
    @WebMethod(operationName = "getAvailablePlaces")
    public int getAvailablePlaces(@WebParam(name = "pt") PackageTourDTO pt) {
        return ejbRef.getAvailablePlaces(pt);
    }
    
    @WebMethod(operationName = "getPackageTour")
    public PackageTourDTO getPackageTour(@WebParam(name = "id") Long id) {
        return (PackageTourDTO) ejbRef.getPackageTour(id);
    }
    
    @WebMethod(operationName = "updatePackageTour")
    @Oneway
    public void updatePackageTour(@WebParam(name = "pt") PackageTourDTO pt) {
        ejbRef.updatePackageTour(pt);
    }
}
